<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-7
 * Time: 下午10:05
 */

namespace backend\controllers;


use common\service\GlobalUrlService;
use common\service\QiniuService;
use common\service\ConstantService;
use common\service\UeditorService;

class UploadController extends BaseController
{
    /**
     * 获取七牛云token
     */
    public function actionGetToken(){
        $file = \Yii::$app->params['qiniu']['file_path'];
        $token = QiniuService::getToken($file);
        $domain = \Yii::$app->params['qiniu']['domain'];
        return $this->renderJson(['token'=>$token,'domain'=>$domain],'获取成功',ConstantService::$response_success);
    }

    /**
     * Ueditor 文件上传与获取
     */
    public function actionUploadImage(){
        $action_name = $this->get('action');

        switch ($action_name)
        {
            case 'config':
                $result = UeditorService::getConfig();
                break;
            /* 上传图片 */
            case 'uploadimage':
                $result = $this->_upload($_FILES);
                break;
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $result = $this->_upload($_FILES);
                break;

            /* 列出图片 */
            case 'listimage':
            /* 列出文件 */
            case 'listfile':
                $result = $this->_list();
                break;

            /* 抓取远程文件 */
            case 'catchimage':
                $result = $this->_crawler();
                break;

            default:
                $result = array(
                    'state'=> '初始化成功'
                );
                break;
        }
        if(!$result){
            $result = array(
            "state" => "ERROR",          //上传状态，上传成功时必须返回"SUCCESS"
            "url" => "",            //返回的地址
            "title" => "",          //新文件名
            "original" => "",       //原始文件名
            "type" => "" ,           //文件类型
            "size" => "",           //文件大小
            );
        }
        echo json_encode($result);
    }

    /**
     * 文件上传
     * @param $files
     * @return array|bool
     */
    private function _upload($files){
        if(empty($files) || !$files['upfile']){
            return false;
        }
        $file = $files['upfile'];
        $filePath = $file['tmp_name'];
        $extArr = \Yii::$app->params['allow_upfile_type']['image'];
        $ext = substr($file['name'],strrpos($file['name'],'.'));

        if(!in_array($ext,$extArr)){
            return false;
        }
        //文件上传
        $filename = \Yii::$app->params['qiniu']['file_path'];
        $result = QiniuService::uploadFile($filePath,$ext,$filename);

        //文件流上传
//        $filename = \Yii::$app->params['qiniu']['file_path'];
//        $stream = file_get_contents($filePath);
//        $result = QiniuService::uploadFileByStream($stream,$ext,$filename);

        if(!$result){
            return false;
        }
        return  [
            "state" => "SUCCESS",          //上传状态，上传成功时必须返回"SUCCESS"
            "url" => GlobalUrlService::buildCdnUrl($result),            //返回的地址
            "title" => $result,          //新文件名
            "original" => $file['name'],       //原始文件名
            "type" => $file['type'] ,           //文件类型
            "size" => $file['size'],           //文件大小
        ];
    }


    private function _list(){

    }

    private function _crawler(){

    }

}